package com.ruoyi.kfwd.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kfwd.mapper.KfwdMapper;
import com.ruoyi.kfwd.domain.Kfwd;
import com.ruoyi.kfwd.service.IKfwdService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 开发文档Service业务层处理
 *
 * @author huacai
 * @date 2024-09-03
 */
@Service
public class KfwdServiceImpl implements IKfwdService
{
    @Autowired
    private KfwdMapper kfwdMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询开发文档
     *
     * @param kfwdId 开发文档主键
     * @return 开发文档
     */
    @Override
    public Kfwd selectKfwdByKfwdId(Integer kfwdId)
    {
        return kfwdMapper.selectKfwdByKfwdId(kfwdId);
    }

    /**
     * 查询开发文档列表
     *
     * @param kfwd 开发文档
     * @return 开发文档
     */
    @Override
    public List<Kfwd> selectKfwdList(Kfwd kfwd)
    {
        return kfwdMapper.selectKfwdList(kfwd);
    }

    /**
     * 新增开发文档
     *
     * @param kfwd 开发文档
     * @return 结果
     */
    @Override
    public int insertKfwd(Kfwd kfwd)
    {
        kfwd.setCreateTime(DateUtils.getNowDate());
        return kfwdMapper.insertKfwd(kfwd);
    }

    /**
     * 批量新增开发文档
     *
     * @param kfwds 开发文档List
     * @return 结果
     */
    @Override
    public int batchInsertKfwd(List<Kfwd> kfwds)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(kfwds)) {
            try {
                for (int i = 0; i < kfwds.size(); i++) {
                    int row = kfwdMapper.insertKfwd(kfwds.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == kfwds.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改开发文档
     *
     * @param kfwd 开发文档
     * @return 结果
     */
    @Override
    public int updateKfwd(Kfwd kfwd)
    {
        kfwd.setUpdateTime(DateUtils.getNowDate());
        return kfwdMapper.updateKfwd(kfwd);
    }

    /**
     * 批量删除开发文档
     *
     * @param kfwdIds 需要删除的开发文档主键
     * @return 结果
     */
    @Override
    public int deleteKfwdByKfwdIds(Integer[] kfwdIds)
    {
        return kfwdMapper.deleteKfwdByKfwdIds(kfwdIds);
    }

    /**
     * 删除开发文档信息
     *
     * @param kfwdId 开发文档主键
     * @return 结果
     */
    @Override
    public int deleteKfwdByKfwdId(Integer kfwdId)
    {
        return kfwdMapper.deleteKfwdByKfwdId(kfwdId);
    }
}
